package com.gengzp.system.controller;

import com.gengzp.common.annotations.OperationLogIgnore;
import com.gengzp.common.exception.BusinessException;
import com.gengzp.common.model.generic.req.SearchByIdReq;
import com.gengzp.common.model.generic.resp.Result;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.common.model.user.req.CreateSysUserReq;
import com.gengzp.common.model.user.req.EditSysUserPasswordReq;
import com.gengzp.common.model.user.req.EditSysUserReq;
import com.gengzp.common.model.user.req.PageSysUserReq;
import com.gengzp.common.model.user.resp.PageUserResp;
import com.gengzp.common.model.user.resp.SearchSysUserResp;
import com.gengzp.permission.annotation.AuthIgnore;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.permission.utils.PermissionUtils;
import com.gengzp.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @ClassName SysUserController
 * @Description 系统用户管理模块控制层
 * @Author gengzp
 * @Date 2025/9/19 13:34
 */
@RestController
@RequestMapping("/user")
@Api(tags = "系统用户管理模块")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增系统用户
     */
    @PostMapping("/create")
    @ApiOperation(value = "新增系统用户")
    @CheckPermission(value = "user:write:create")
    public Result<Void> addSysUser(@Valid @RequestBody CreateSysUserReq req) {
        sysUserService.createSysUser(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑系统用户
     */
    @PostMapping("/edit_by_id")
    @ApiOperation(value = "编辑系统用户")
    public Result<Void> editSysUserById(@Valid @RequestBody EditSysUserReq req) {
        // 没有user-write权限的用户只能修改自己的信息;
        if (!PermissionUtils.verifyUserIsSuperAdmin(PermissionUtils.getCurrentUserId()) &&
                !PermissionUtils.getCurrentUserOwnedPermissionCodes().contains("user:write:create") &&
                !Objects.equals(req.getId(), PermissionUtils.getCurrentUserId())) {
            return Result.getFailResult("没有权限修改此用户信息");
        }
        // 超级管理员信息不允许修改
        if (Objects.equals(req.getId(), PermissionUtils.getSuperAdminUserId())) {
            throw BusinessException.get("超级管理员用户信息不允许修改");
        }

        sysUserService.editSysUserById(req);
        return Result.getSuccessResult();
    }

    /**
     * 修改用户密码
     */
    @PostMapping("/edit_password")
    @ApiOperation(value = "修改用户密码")
    public Result<Void> editSysUserPassword(@Valid @RequestBody EditSysUserPasswordReq req) {
        // 所有用户都只能修改自己的密码
        if (!Objects.equals(PermissionUtils.getCurrentUserId(), req.getId())) {
            return Result.getFailResult("没有权限修改此用户密码");
        }
        // 超级管理员的密码从配置文件读取, 不允许修改
        if (Objects.equals(req.getId(), PermissionUtils.getSuperAdminUserId())) {
            throw BusinessException.get("超级管理员用户密码不允许修改");
        }

        sysUserService.editSysUserPasswordById(req);
        return Result.getSuccessResult();
    }

    /**
     * 根据id删除系统用户
     */
    @PostMapping("/remove_by_id")
    @ApiOperation(value = "根据id删除系统用户")
    @CheckPermission(value = "user:write:remove")
    public Result<Void> removeSysUserById(@Valid @RequestBody SearchByIdReq req) {
        sysUserService.removeSysUserById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 根据id查询用户详情
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询用户详情")
    public Result<SearchSysUserResp> searchSysUserById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(sysUserService.searchSysUserById(Long.valueOf(req.getId())));
    }

    /**
     * 分页查询系统用户
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/page")
    @ApiOperation(value = "分页查询系统用户")
    public Result<PaginationResponse<PageUserResp>> pageQuerySysUser(@RequestBody PageSysUserReq req) {
        return Result.getSuccessResult(sysUserService.pageQuerySysUser(req));
    }

}
